(function (win) {
    axios.defaults.headers['Content-Type'] = 'application/json;charset=utf-8'
    const service = axios.create()
    service.interceptors.request.use(config => {
        console.log("请求拦截")
        if (config.method === 'get' && config.params) {
            let url = config.url + '?';
            for (const propName of Object.keys(config.params)) {
                const value = config.params[propName];
                var part = encodeURIComponent(propName) + "=";
                if (value !== null && typeof(value) !== "undefined") {
                    if (typeof value === 'object') {
                        for (const key of Object.keys(value)) {
                            let params = propName + '[' + key + ']';
                            var subPart = encodeURIComponent(params) + "=";
                            url += subPart + encodeURIComponent(value[key]) + "&";
                        }
                    } else {
                        url += part + encodeURIComponent(value) + "&";
                    }
                }
            }
            url = url.slice(0, -1);
            config.params = {};
            config.url = url;
        }

        return config
    }, error => {
        console.log(error)
        Promise.reject(error)
    })

    service.interceptors.response.use(response => {
        console.log("响应拦截")
        if (response.data.code ===0 && response.data.msg === 'NOTLOGIN') {
            window.top.location.href = '/pages/front/login.html'
        }
        else {
            return response
        }
        },
        error => {
            return Promise.reject(error)
        }

    )
    win.axios = service
})(window);
